import { defaultSettings } from "@/utils/bpmn/editor.config";
import { unObserver } from "@/utils/bpmn/tool";

const bpmn = {
  state: {
    bpmn: {
      _modeler: null,
      activeElement: null
    },
    editor: { ...defaultSettings },
  },
  mutations: {
    /**
     * 设置当前活动元素
     * @param {*} state 
     * @param {*} param1 
     * * element-元素
     * * id-ID值
     */
    setElement(state, { element, id }) {
      state.bpmn.activeElement =  { element: unObserver(element), id };
    },
    /**
     * 清空 Bpmn 元素
     * @param {*} state 
     */
    clearBpmnState(state) {
      state.bpmn = {};
    },
    /**
     * 设置 Bpmn 配置文件
     * @param {*} state 
     * @param {*} conf 新配置
     */
    setConfiguration(state, conf) {
      state.editor = { ...state.editor, ...conf };
    },
    /**
     * 设置 Bpmn 建模器
     * @param state
     * @param modeler { object }
     */
     setModeler(state, modeler) {
      state.bpmn._modeler = unObserver(modeler);
    },
  },
  getters: {
    // 编辑
    getEditor: (state) => state.editor,
    // 编辑配置信息
    getEditorConfig: (state) => {
      return Object.keys(state.editor).reduce((config, key) => {
        if (!["processName", "processId", "processEngine"].includes(key)) {
          config[key] = state.editor[key];
        }
        return config;
      }, {});
    },

    // 建模器相关
    getModeler: (state) => state.bpmn._modeler,
    getModeling: (state) => (state.bpmn._modeler ? state.bpmn._modeler.get("modeling") : undefined),
    getActiveEl: (state) => state.bpmn.activeElement
  },
};

export default bpmn
